<?php

namespace app\index\controller;

use app\index\model\Index as IndexModel;
use think\facade\Cookie;
use think\facade\Request;

class Index extends BaseController {

    public function index(int $source = 1) {
        //$source = (int)$source;
        //检测手机端
        if($this->ismobile()){
            $this->redirect('http://m.xiaojuan.fun');
        }

        $sourcefromsys = config('source');
        $source = $sourcefromsys[$source];

        if(Request::isAjax()){
            if($source != 1){
                Cookie::set('source', $source, config('source_time'));
            }elseif($source === 0){
                return json(['msg'=>'暂无来源信息，请选择其他来源！', 'code'=>403]);
            }

            $index = IndexModel::getIndex($source, $page = 1);
        }else{
            $source = 'zwdu';

            $index = IndexModel::getIndex($source, $page = 1);

        }
        //dump($index);exit;
        $this->assign('index', $index);
        return $this->fetch('index/index');
    }

    public function category(int $id){
        return $id;
    }
}
